﻿using System;
using System.Collections;
using System.Data;

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Text;
using TimKen.BussLogic;
using System.Threading;

namespace TimKen.Web.Ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class TypeOption : IHttpHandler
    {
        ProductManager pm = new ProductManager();
        PCategoryManager ptm = new PCategoryManager();

        #region IHttpHandle
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string productid = HttpContext.Current.Request.QueryString["ProductID"];
          
            DataSet ds = new DataSet();
            DataTable dt = null;
            if (!string.IsNullOrEmpty(productid))
            {
                ds = pm.GetAttributeWithValueByProductID(productid);
                dt = ConvertTable(ds.Tables[0]);
            }
            context.Response.Write(ProcessOption(dt));
        }







        public DataTable ConvertTable(DataTable odt)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OptionID");
            dt.Columns.Add("OptionValue");
            dt.Columns.Add("FieldMetaData");
            dt.Columns.Add("FieldType");
            dt.Columns.Add("OptionName");
            dt.Columns.Add("LanguageID");

            //中文属性
            DataRow[] drs = odt.Select("LanguageID="+GetLanguageID());
            foreach (DataRow dr in drs)
            {
                DataRow ndr = dt.NewRow();
                ndr["OptionID"] = dr["OptionID"];

                ndr["FieldMetaData"] = dr["FieldMetaData"];
                ndr["FieldType"] = dr["FieldType"];
                ndr["OptionName"] = dr["OptionName"];
                ndr["LanguageID"] = dr["LanguageID"];
                if (odt.Columns.Contains("OptionValue"))
                {
                    ndr["OptionValue"] = dr["OptionValue"];
                }
                else
                    ndr["OptionValue"] = "";


                dt.Rows.Add(ndr);
            }
            return dt;
        }



        /// <summary>
        /// 获取语言
        /// </summary>
        /// <returns></returns>
        private string GetLanguageID()
        {
            switch (Thread.CurrentThread.CurrentUICulture.ToString())
            {
                case "zh-CN": return "1";
                case "en-US": return "2";
                default: return "1";
            }
        }



        private string ProcessOption(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];

                if (i % 2 == 1)
                    sb.Append("<li >");

                else
                    sb.Append("<li class='bggray'>");

                sb.Append(dr["OptionName"].ToString() + "：");
                sb.Append(dr["OptionValue"].ToString());
                sb.Append("</li>");

            }
            return sb.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion


    }
}
